package io.openkunlun.javadsl;

import io.grpc.Status;

public class DaprException extends RuntimeException {

    private String code;
    private String message;

    public DaprException(Throwable e) {
        super(e);
    }

    public DaprException(String code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public DaprException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return this.code;
    }

    public String message() {
        return this.message;
    }

    /**
     * @param throwable
     * @return
     */
    public static DaprException fromThrowable(Throwable throwable) {
        if (throwable instanceof DaprException) {
            return (DaprException) throwable;
        }

        Throwable error = Status.fromThrowable(throwable).asException();
        return new DaprException(error);
    }
}
